package main

import (
	"cyrus/rabbit-go/conf"
	"log"

	amqp "github.com/rabbitmq/amqp091-go"
)

func main() {
	if err := conf.Init(); err != nil {
		log.Fatal("config init failed:", err)
	}

	conn, err := amqp.Dial(conf.Cfg.Rabbit.Dsn)
	if err != nil {
		log.Fatal("connect to RabbitMQ failed:", err)
	}
	defer conn.Close()

	ch, err := conn.Channel()
	if err != nil {
		log.Fatal("open channel failed:", err)
	}
	defer ch.Close()

	q, err := ch.QueueDeclare(
		"hello", //name
		false,   // durable
		false,   // deted when unused
		false,   // exclusive
		false,   // no-wait
		nil,     // args
	)
	if err != nil {
		log.Fatal("declare queue failed:", err)
	}

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		true,   // auto-ack
		false,  // exclusive
		false,  //no-local
		false,  //no-wait
		nil,    //args
	)
	if err != nil {
		log.Fatal("register consumer failed:", err)
	}

	var forever chan struct{}

	go func() {
		for d := range msgs {
			log.Printf("Received a message: %s\n", d.Body)
		}
	}()

	log.Println(" [*] Wating for messages. To exit press CTRL-C")
	<-forever
}
